/**  
 * @Title: JobService.java
 * @Description: TODO(描述)
 * @author liuyi
 * @date 2023-06-26 08:30:04 
 */  
package com.fsxgt.datagrid.sys.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import com.fsxgt.datagrid.core.exception.DataGridException;
import com.fsxgt.datagrid.sys.bo.LoginBO;
import com.fsxgt.datagrid.sys.bo.SysUserBO;
import com.fsxgt.datagrid.sys.dao.SysUserMapper;
import com.fsxgt.datagrid.sys.utils.MD5Util;

import lombok.extern.slf4j.Slf4j;

/**  
 * @ClassName: JobService
 * @Description: TODO(描述)
 * @author liuyi
 * @date 2023-06-26 08:30:04 
*/
@Service
@Slf4j
public class SysUserService implements UserDetailsService {
	
	private final static String  PASSWORK="123456";
	
	@Autowired
    private SysUserMapper sysUserMapper;

	

	/**  
	 * @Title: loadUserByUsername
	 * @Description: TODO(描述)
	 * @param username
	 * @return
	 * @throws UsernameNotFoundException 
	 * @see org.springframework.security.core.userdetails.UserDetailsService#loadUserByUsername(java.lang.String) 
	 * @author liuyi
	 * @date 2023-09-20 09:15:50 
	 */ 
	@Override
	public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
		// TODO Auto-generated method stub

		SysUserBO sysUserBO = sysUserMapper.getSysUserByUsername(username);
        // 判断用户是否存在
        if(sysUserBO == null) {
            return null;
        }

        return new LoginBO(sysUserBO);
        
	}

	/**  
	 * @Title: deleteSysUser
	 * @Description: TODO(描述)
	 * @param id
	 * @author liuyi
	 * @date 2023-09-22 16:32:42 
	 */  
	public void deleteSysUser(Long id) {
		// TODO Auto-generated method stub
		sysUserMapper.deleteSysUser(id);
	}

	/**  
	 * @Title: getSysUser
	 * @Description: TODO(描述)
	 * @param sysUserBO
	 * @return
	 * @author liuyi
	 * @date 2023-09-22 16:33:21 
	 */  
	public SysUserBO getSysUser(Long id) {
		// TODO Auto-generated method stub
		return sysUserMapper.getSysUser(id);
	}

	/**  
	 * @Title: modifySysUser
	 * @Description: TODO(描述)
	 * @param sysUserBO
	 * @author liuyi
	 * @throws DataGridException 
	 * @date 2023-09-22 16:34:16 
	 */  
	public void modifySysUser(SysUserBO sysUserBO) throws DataGridException {
		// TODO Auto-generated method stub
		
		SysUserBO user = sysUserMapper.getSysUserByUsername(sysUserBO.getUsername());

		if(user!=null&&!user.getId().equals(sysUserBO.getId())) {
			throw new DataGridException("已经存在"+sysUserBO.getUsername());
		}
		sysUserMapper.updateSysUser(sysUserBO);
	}

	/**  
	 * @Title: addSysUser
	 * @Description: TODO(描述)
	 * @param sysUserBO
	 * @author liuyi
	 * @throws DataGridException 
	 * @date 2023-09-22 16:34:39 
	 */  
	public void addSysUser(SysUserBO sysUserBO) throws DataGridException {
		// TODO Auto-generated method stub
		if(sysUserMapper.getSysUserByUsername(sysUserBO.getUsername())!=null) {
			throw new DataGridException("已经存在"+sysUserBO.getUsername());
		}
		sysUserBO.setPassword(MD5Util.encode(PASSWORK));
		sysUserMapper.insertSysUser(sysUserBO);
	}
	
	/**  
	 * @Title: modifyPasswork
	 * @Description: TODO(描述)
	 * @param sysUserBO
	 * @author liuyi
	 * @param string 
	 * @throws DataGridException 
	 * @date 2023-09-25 10:51:24 
	 */  
	

	
	public void modifyPassword(Long id,String oldpassword, String newpassword) throws DataGridException {
		// TODO Auto-generated method stub
		
		SysUserBO user = getSysUser(id);
		
		if(!user.getPassword().equals(MD5Util.encode(oldpassword))) {
			throw new DataGridException("密码错误");
		}
		user.setPassword(MD5Util.encode(newpassword));
		sysUserMapper.updatePassword(user);
	}
	
	public void resetPassword(SysUserBO sysUserBO) {
		// TODO Auto-generated method stub
		sysUserBO.setPassword(MD5Util.encode(PASSWORK));
		sysUserMapper.updatePassword(sysUserBO);
	}

	/**  
	 * @Title: findSysUserPage
	 * @Description: TODO(描述)
	 * @param param
	 * @return
	 * @author liuyi
	 * @date 2023-09-22 16:36:18 
	 */  
	public List<SysUserBO> findSysUserPage(SysUserBO sysUserBO) {
		// TODO Auto-generated method stub
		return sysUserMapper.findSysUser(sysUserBO);
	}
	
	/**  
	 * @Title: findSubject
	 * @Description: TODO(描述)
	 * @return
	 * @author liuyi
	 * @date 2023-09-25 08:40:25 
	 */  
	public List findSubject() {
		// TODO Auto-generated method stub
		return sysUserMapper.findRootMenu();
	}




	
	public static void main(String args[]) {
		System.out.print(MD5Util.encode("pear"));
	}
	
	


	

}
